//
//  TestElementsViewController.m
//  MKUITestApp
//
//  Created by 苏尚进 on 2023/7/10.
//  Copyright © 2023 moon. All rights reserved.
//

#import "TestElementsViewController.h"
#import "Menu.h"
#import "OpertaionBlock.h"
#import "MKUIElementListViewCellSwitch+Test.h"
#import "MKUIElementListViewCellSelection+Test.h"
#import "TestMKUIListViewSectionHeadFootView.h"
#import "MKUIElementListViewCellButton+Test.h"

@interface TestElementsView_GroupBackgrounView : UIView
@property(nonatomic,strong) UIVisualEffectView *backgroundEffectView;//半透明效果

@end
@implementation TestElementsView_GroupBackgrounView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.backgroundColor = UIColor.redColor;
        self.backgroundEffectView = [[UIVisualEffectView alloc] init];
        [self addSubview:self.backgroundEffectView];
        [self _MKUICustomTabBar_doTraitCollection];
    }
    return self;
}
- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection{
    [super traitCollectionDidChange:previousTraitCollection];
    [self _MKUICustomTabBar_doTraitCollection];
}
- (void)layoutSubviews{
    [super layoutSubviews];
    self.backgroundEffectView.frame = self.bounds;
}
- (void)_MKUICustomTabBar_doTraitCollection{
    if (@available(iOS 12.0, *)) {
        if(self.traitCollection.userInterfaceStyle==UIUserInterfaceStyleDark){
            self.backgroundEffectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        }else{
            self.backgroundEffectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        }
    }else{
        self.backgroundEffectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    }
}
@end

@interface TestElementsView_SeparatorView : UIView
@end
@implementation TestElementsView_SeparatorView
- (id)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.backgroundColor = UIColor.systemYellowColor;
    }
    return self;
}
- (CGSize)sizeThatFits:(CGSize)size{
    return CGSizeMake(size.width, 2);
}
@end

@interface TestElementsViewController ()<MKUIListViewDelegate>
@property(nonatomic,strong) UITapGestureRecognizer *tapGesture;

@property(nonatomic,strong) UIColor *custom_groupBackgroundColor;
@property(nonatomic,assign) UIEdgeInsets custom_groupInsets;
@property(nonatomic,assign) UIEdgeInsets custom_groupInnerInsets;
@property(nonatomic,assign) CGFloat custom_groupCornerRadius;
@property(nonatomic,assign) CGFloat custom_cellInteritemSpacing;

@property(nonatomic,assign) MKUIListViewCellSeparatorStyle custom_separatorStyle;
@property(nonatomic,strong) UIColor *custom_separatorColor;
@property(nonatomic,strong) UIColor *custom_separatorBackgroundColor;
@property(nonatomic,assign) CGFloat custom_separatorSize;
@property(nonatomic,assign) UIEdgeInsets custom_separatorInsets;
@property(nonatomic,strong) Class custom_separatorViewClass;
@property(nonatomic,assign) NSInteger custom_section;
@property(nonatomic,readonly) BOOL isListViewDelegateSetted;
@property(nonatomic,assign) Class custom_groupBackgroudViewClass;
@end

@implementation TestElementsViewController
+ (MKTFuncItem *)funcItem{
    return [MKTFuncItem funcWithName:@"ElementList表单" viewControllerClass:self];
}
//- (void)viewWillLayoutSubviews{
//    [super viewWillLayoutSubviews];
//    CGRect bounds = self.view.bounds;
//    UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 100, 0);
//    CGRect f1 = UIEdgeInsetsInsetRect(bounds, insets);
//    self.listView.frame = f1;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.custom_groupInsets = self.listView.groupInsets;
    self.custom_groupInnerInsets = self.listView.groupInnerInsets;
    self.custom_separatorSize = self.listView.separatorSize;
    self.custom_separatorColor = self.listView.separatorColor;
    self.custom_separatorStyle = self.listView.separatorStyle;
    self.custom_separatorInsets = self.listView.separatorInsets;
    self.custom_groupCornerRadius = self.listView.groupCornerRadius;
    self.custom_separatorViewClass = self.listView.separatorViewClass;
    self.custom_cellInteritemSpacing = self.listView.cellInteritemSpacing;
    self.custom_groupBackgroundColor = self.listView.groupBackgroundColor;
    self.custom_separatorBackgroundColor = self.listView.separatorBackgroundColor;
    
    self.listView.style = MKUIListViewStyleInsetGrouped;
    self.listView.delegate = nil;
    [self.listView deleteAllSections];
    @MK_WEAKIFY(self);
    {
        TestMKUIListViewSectionHeadFootView *head = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"listView的head"];
        self.listView.listHeaderView = head;
        TestMKUIListViewSectionHeadFootView *foot = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"listView的Foot"];
        self.listView.listFooterView = foot;
    }
    [self.listView addSection];
    [self.listView addSectionWithHeadView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"分组A的head"] footView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"分组A的Foot"]];
    [self.listView addSection];
    [self.listView addSectionWithHeadView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"分组B的head"] footView:nil];
    [self.listView addSection];
    [self.listView addSectionWithHeadView:nil footView:[TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"分组C的Foot"]];
    
    [self.listView addSection];
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"是否使用PlainStyle" on:self.listView.style==MKUIListViewStylePlain whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.listView.style = on?MKUIListViewStylePlain:MKUIListViewStyleInsetGrouped;
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置sectionInteritemSpacing" selected:self.listView.sectionInteritemSpacing whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.listView.sectionInteritemSpacing = selectedValue;
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置Delegate" on:self.isListViewDelegateSetted whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.listView.delegate = on?self:nil;
            [self.listView reloadData];
        }];
        [self.listView addItem:el];
    }
    [self _testElementCustomSection];
    [self _testElementSetSectionPropertis];
    [self _testElementText];
    [self _testElementImage];
    [self _testSwitchElements];
    [self _testElementSelect];
    [self _testElementTextField];
    [self _testElementTextArea];
    [self _testElementTextInput];
    [self _testElementButton];
    
    [self.listView addSection];
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [MKUIElementListViewCellButton testButtonElementWithTitle:@"刷新" buttonStyle:(MKUIElementViewButtonStyleDefault) whenClick:^(MKUIElementListViewCellButton * _Nonnull el) {
            @MK_NORMALIZE(self);
            [self.listView reloadData];
        }];
        [self.listView addItem:el];
    }
    [self.listView addSection];
    [self.listView addSection];
    [self.listView reloadData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__onKeyboardShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__onKeyboardChange:) name:UIKeyboardDidChangeFrameNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(__onKeyboardHide:) name:UIKeyboardDidHideNotification object:nil];
    //
    self.tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(__onTap:)];
    self.tapGesture.enabled = NO;
    [self.view addGestureRecognizer:self.tapGesture];
}
- (void)__onTap:(id)sender{
    [self.view endEditing:YES];
}
- (void)__onKeyboardShow:(NSNotification *)noti{
    self.tapGesture.enabled = YES;
    [self.listView mk_adjustContentWithUIKeyboardDidShowNotification:noti responderViewClass:[MKUIListViewAttributeContainerView class] contentInsets:UIEdgeInsetsZero window:UIApplication.sharedApplication.keyWindow];
}
- (void)__onKeyboardChange:(NSNotification *)noti{
    [self.listView mk_adjustContentWithUIKeyboardDidShowNotification:noti responderViewClass:[MKUIListViewAttributeContainerView class] contentInsets:UIEdgeInsetsZero window:UIApplication.sharedApplication.keyWindow];
}
- (void)__onKeyboardHide:(NSNotification *)noti{
    self.tapGesture.enabled = NO;
    UIEdgeInsets insets = self.listView.contentInset;
    insets.bottom = 0;
    self.listView.contentInset = insets;
}
- (BOOL)isListViewDelegateSetted{
    return self.listView.delegate!=nil;
}
- (void)_testElementSetSectionPropertis{
    [self.listView addSection];
    NSInteger section = self.listView.numberOfSections;
    @MK_WEAKIFY(self);
    {
        TestMKUIListViewSectionHeadFootView *head = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"单独设置该分组的属性Begin"];
        [self.listView setSectionView:head atIndex:section kind:(MKUIListViewSectionViewKindOfHead)];
        TestMKUIListViewSectionHeadFootView *foot = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"单独设置该分组的属性End"];
        [self.listView setSectionView:foot atIndex:section kind:(MKUIListViewSectionViewKindOfFoot)];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"是否使用自定义groupBackgroundClass" on:[self.listView groupBackgroundColorForSection:section]!=nil whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            Class custom_groupBackgroudViewClass = on?TestElementsView_GroupBackgrounView.class:nil;
            [self.listView setGroupBackgroundViewClass:custom_groupBackgroudViewClass forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置groupBackgroundColor" selected:[self.listView groupBackgroundColorForSection:section] whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setGroupBackgroundColor:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIEdgeInsetsElementWithTitle:@"设置groupInsets" selected:[self.listView groupInsetsForSection:section] whenDone:^(UIEdgeInsets selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setGroupInsets:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIEdgeInsetsElementWithTitle:@"设置groupInnerInsets" selected:[self.listView groupInnerInsetsForSection:section] whenDone:^(UIEdgeInsets selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setGroupInnerInsets:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置groupCornerRadius" selected:[self.listView groupCornerRadiusForSection:section] whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setGroupCornerRadius:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置cellInteritemSpacing" selected:[self.listView cellInteritemSpacingForSection:section] whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setCellInteritemSpacing:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testElementMKUIListViewCellSeparatorStyle:@"设置separatorStyle" selected:[self.listView separatorStyleForSection:section] whenDone:^(MKUIListViewCellSeparatorStyle selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorStyle:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置separatorColor" selected:[self.listView separatorColorForSection:section] whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorColor:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置separatorBackgroundColor" selected:[self.listView separatorBackgroundColorForSection:section] whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorBackgroundColor:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置separatorSize" selected:[self.listView separatorSizeForSection:section] whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorSize:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIEdgeInsetsElementWithTitle:@"设置separatorInsets" selected:[self.listView separatorInsetsForSection:section] whenDone:^(UIEdgeInsets selectedValue) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorInsets:selectedValue forSection:section];
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置separatorViewClass" on:[self.listView separatorViewClassForSection:section]!=nil whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            [self.listView setSeparatorViewClass:on?TestElementsView_SeparatorView.class:nil forSection:section];
        }];
        [self.listView addItem:el];
    }
}
- (void)_testElementCustomSection{
    [self.listView addSection];
    self.custom_section = self.listView.numberOfSections;
    @MK_WEAKIFY(self);
    {
        TestMKUIListViewSectionHeadFootView *head = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"设置Delegate的属性Begin"];
        [self.listView setSectionView:head atIndex:self.custom_section kind:(MKUIListViewSectionViewKindOfHead)];
        TestMKUIListViewSectionHeadFootView *foot = [TestMKUIListViewSectionHeadFootView sectionViewWithTitle:@"设置Delegate的属性End"];
        [self.listView setSectionView:foot atIndex:self.custom_section kind:(MKUIListViewSectionViewKindOfFoot)];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"是否使用自定义groupBackgroundClass" on:self.custom_groupBackgroudViewClass!=nil whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.custom_groupBackgroudViewClass = on?TestElementsView_GroupBackgrounView.class:nil;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置groupBackgroundColor" selected:self.custom_groupBackgroundColor whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_groupBackgroundColor = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIEdgeInsetsElementWithTitle:@"设置groupInsets" selected:self.custom_groupInsets whenDone:^(UIEdgeInsets selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_groupInsets = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIEdgeInsetsElementWithTitle:@"设置groupInnerInsets" selected:self.custom_groupInnerInsets whenDone:^(UIEdgeInsets selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_groupInnerInsets = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置groupCornerRadius" selected:self.custom_groupCornerRadius whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_groupCornerRadius = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置cellInteritemSpacing" selected:self.custom_cellInteritemSpacing whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_cellInteritemSpacing = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testElementMKUIListViewCellSeparatorStyle:@"设置separatorStyle" selected:self.custom_separatorStyle whenDone:^(MKUIListViewCellSeparatorStyle selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_separatorStyle = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置separatorColor" selected:self.custom_separatorColor whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_separatorColor = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIColorElementWithTitle:@"设置separatorBackgroundColor" selected:self.custom_separatorBackgroundColor whenDone:^(UIColor * _Nonnull selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_separatorBackgroundColor = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testNSIntegerElementWithTitle:@"设置separatorSize" selected:self.custom_separatorSize whenDone:^(NSInteger selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_separatorSize = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [MKUIElementListViewCellSelection testUIEdgeInsetsElementWithTitle:@"设置separatorInsets" selected:self.custom_separatorInsets whenDone:^(UIEdgeInsets selectedValue) {
            @MK_NORMALIZE(self);
            self.custom_separatorInsets = selectedValue;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [MKUIElementListViewCellSwitch testSwitchElementWithTitle:@"设置separatorViewClass" on:self.custom_separatorViewClass!=nil whenChange:^(BOOL on) {
            @MK_NORMALIZE(self);
            self.custom_separatorViewClass = on?TestElementsView_SeparatorView.class:nil;
            if(self.isListViewDelegateSetted){
                [self.listView reloadData];
            }
        }];
        [self.listView addItem:el];
    }
    
}
- (void)_testElementText{
    [self.listView addSection];
    [self.listView addSection];
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"标题1";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.detailText = @"详细内容1";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.whenClick = ^(MKUIElementListViewCellDetail * _Nonnull el) {
            el.elementView.accessView = el.elementView.accessView?nil:el.elementView.disclosureIndicatorAccessView;
            el.elementView.subTitleText = el.elementView.subTitleText.length>0?nil:@"子标题1";
            if(UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, el.elementView.iconImageMarggin)){
                el.elementView.iconImageSize = CGSizeMake(60, 30);
                el.elementView.iconImageMarggin = UIEdgeInsetsMake(10, 10, 10, 10);
            }else{
                el.elementView.iconImageSize = CGSizeMake(28, 28);
                el.elementView.iconImageMarggin = UIEdgeInsetsZero;
            }
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    [self.listView addSection];
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"标题1";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.detailText = @"详细内容1";
        el.elementView.accessView = el.elementView.checkmarkAccessView;
        el.whenClick = ^(MKUIElementListViewCellDetail * _Nonnull el) {
            el.elementView.detailText = [NSString stringWithFormat:@"%@详细内容1",el.elementView.detailText];
            el.elementView.accessView = el.elementView.accessView?nil:el.elementView.checkmarkAccessView;
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellDetail *el = [[MKUIElementListViewCellDetail alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"标题1";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.detailText = @"详细内容1";
        el.elementView.accessView = el.elementView.checkmarkAccessView;
        el.whenClick = ^(MKUIElementListViewCellDetail * _Nonnull el) {
            el.elementView.detailText = [NSString stringWithFormat:@"%@详细内容1",el.elementView.detailText];
            el.elementView.accessView = el.elementView.accessView?nil:el.elementView.checkmarkAccessView;
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testSwitchElements{
    [self.listView addSection];
    if (@available(iOS 13.0, *)){
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"是否黑暗模式";
        el.elementView.on = self.traitCollection.userInterfaceStyle==UIUserInterfaceStyleDark;
        @MK_WEAKIFY(self);
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.overrideUserInterfaceStyle = on?UIUserInterfaceStyleDark:UIUserInterfaceStyleLight;
        };
        [self.listView addItem:el];
    }
    if (@available(iOS 13.0, *)){
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.titleText = @"group背景透明";
        el.elementView.on = NO;
        @MK_WEAKIFY(self);
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.listView.groupBackgroundColor = on?nil:UIColor.mk_listViewGroupBackgroundColor;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"listView分隔线颜色";
        @MK_WEAKIFY(self);
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el, BOOL on) {
            @MK_NORMALIZE(self);
            self.listView.separatorColor = on?[UIColor mk_colorWithLight:UIColor.systemRedColor dark:UIColor.systemBlueColor]:UIColor.mk_listViewSeparatorColor;
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSwitch *el = [[MKUIElementListViewCellSwitch alloc] init];
        el.elementView.iconImage = nil;
        el.elementView.titleText = @"Switch标题";
        el.elementView.on = YES;
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^(MKUIElementViewSwitch * _Nonnull el2, BOOL on) {
            @MK_NORMALIZE(el);
            el.elementView.hiddenEmptyIconImage = el.elementView.isOn;
            el.elementView.subTitleText = el.elementView.isOn?@"Switch选中":@"Switch未选中";
            [el mk_reloadInListView];
        };

        //CGColor不能使用动态颜色，不会随黑暗模式变更而变色。
        MKUIFlowLayoutButton *button = [[MKUIFlowLayoutButton alloc] init];
        button.layer.borderWidth = 1;
        button.layer.borderColor = [UIColor mk_colorWithLight:UIColor.redColor dark:UIColor.blueColor].CGColor;
        [button setTitleColor:[UIColor mk_colorWithLight:UIColor.redColor dark:UIColor.blueColor] forState:(UIControlStateNormal)];
        [button setTitle:@"按钮" forState:(UIControlStateNormal)];
        button.frame = CGRectMake(0, 0, 60, 40);
        el.elementView.accessView = button;
        [self.listView addItem:el];
    }
}
- (void)_testElementButton{
    [self.listView addSection];
    {
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"Button标题1";
        el.whenClick = ^(MKUIElementListViewCellButton * _Nonnull el) {
            el.elementView.titleText = [NSString stringWithFormat:@"%@%@",el.elementView.titleText,el.elementView.titleText];
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }{
        MKUIElementListViewCellButton *el = [[MKUIElementListViewCellButton alloc] init];
        el.elementView.titleText = @"Button标题1";
        el.whenClick = ^(MKUIElementListViewCellButton * _Nonnull el) {
            el.elementView.buttonStyle = (el.elementView.buttonStyle+1)%3;
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementSelect{
    [self.listView addSection];

    NSArray<MKUIElementSelectOption *> *options = [MKUIElementSelectOption optionsWithTexts:@[
        @"选项1",
        @"选项2",
        @"选项3",
        @"选项4",
    ]];
    {
        MKUIElementListViewCellSelection *el = [[MKUIElementListViewCellSelection alloc] init];
        el.elementView.titleText = @"单选2";
        el.elementView.allowsMultipleSelection = NO;
        el.elementView.options = options;
        el.elementView.doneButtonText = @"完成";
        el.elementView.selectedValue = options.lastObject.value;
        @MK_WEAKIFY(el);
        el.elementView.whenDone = ^(__kindof MKUIElementViewSelection * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"selected:%@",el.elementView.selectedValue);
            [el mk_reloadInListView];
        };  
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellSelection *el = [[MKUIElementListViewCellSelection alloc] init];
        el.elementView.titleText = @"多选2";
        el.elementView.allowsMultipleSelection = YES;
        el.elementView.options = options;
        el.elementView.detailTextWhenNoSelected = @"未选择";
        @MK_WEAKIFY(el);
        el.elementView.whenDone = ^(__kindof MKUIElementViewSelection * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"selected:%@",[el.elementView.selectedValues componentsJoinedByString:@","]);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementTextField{
    {
        [self.listView addSection];
        MKUIElementListViewCellTextField *el = [[MKUIElementListViewCellTextField alloc] init];
        el.elementView.textField.textAlignment = NSTextAlignmentLeft;
        el.elementView.textField.placeholder = @"请输入";
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^(__kindof MKUIElementViewTextField * _Nonnull el2, NSString * _Nonnull text) {
            @MK_NORMALIZE(el);
            NSLog(@"text:%@",text);
            [el mk_reloadInListView];
        };
        el.elementView.whenReturn = ^(MKUIElementViewTextField * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"return text:%@",el.elementView.text);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        [self.listView addSection];
        MKUIElementListViewCellTextField *el = [[MKUIElementListViewCellTextField alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"输入单行文本";
        el.elementView.textField.placeholder = @"请输入";
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^(__kindof MKUIElementViewTextField * _Nonnull el2, NSString * _Nonnull text) {
            @MK_NORMALIZE(el);
            NSLog(@"text:%@",text);
            if(text.length>20){
                text = [text substringToIndex:20];
                el.elementView.text = text;
            }
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementTextArea{
    {
        [self.listView addSection];
        MKUIElementListViewCellTextArea *el = [[MKUIElementListViewCellTextArea alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"输入多行文本";
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^(__kindof MKUIElementViewTextArea * _Nonnull el2, NSString * _Nonnull text) {
            @MK_NORMALIZE(el);
            NSLog(@"text:%@",text);
            if(text.length>20){
                text = [text substringToIndex:20];
                el2.text = text;
            }
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        [self.listView addSection];
        MKUIElementListViewCellTextArea *el = [[MKUIElementListViewCellTextArea alloc] init];
        el.elementView.iconImage = nil;
        el.elementView.titleText = nil;
        el.elementView.textView.textAlignment = NSTextAlignmentLeft;
        el.elementView.doneButtonText = @"完成";
        @MK_WEAKIFY(el);
        el.elementView.whenDone = ^(__kindof MKUIElementViewTextArea * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"text view done:%@",el2.text);
            [el mk_reloadInListView];
        };
        el.elementView.whenChange = ^(__kindof MKUIElementViewTextArea * _Nonnull el2, NSString * _Nonnull text) {
            @MK_NORMALIZE(el);
            NSLog(@"text:%@",text);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementTextInput{
    [self.listView addSection];
    {
        MKUIElementListViewCellTextInput *el = [[MKUIElementListViewCellTextInput alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"输入单行文本";
        el.elementView.placeholder = @"请输入";
        el.elementView.allowsMultipleLineText = NO;
//        @MK_WEAKIFY(self);
        @MK_WEAKIFY(el);
        el.elementView.whenChange = ^NSString * _Nonnull(__kindof MKUIElementViewTextInput * _Nonnull el2, NSString * _Nonnull text) {
//            @MK_NORMALIZE(self);
            if(text.length>20){
                return [text substringToIndex:20];
            }
            return text;
        };
        el.elementView.whenDone = ^(__kindof MKUIElementViewTextInput * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"done:%@",el2.text);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellTextInput *el = [[MKUIElementListViewCellTextInput alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"输入多行文本";
        el.elementView.allowsMultipleLineText = YES;
        el.elementView.doneButtonText = @"完成";
//        @MK_WEAKIFY(self);
        el.elementView.whenChange = ^NSString * _Nonnull(__kindof MKUIElementViewTextInput * _Nonnull el, NSString * _Nonnull text) {
//            @MK_NORMALIZE(self);
            NSLog(@"change:%@",text);
            return text;
        };
        @MK_WEAKIFY(el);
        el.elementView.whenDone = ^(__kindof MKUIElementViewTextInput * _Nonnull el2) {
            @MK_NORMALIZE(el);
            NSLog(@"done:%@",el2.text);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
- (void)_testElementImage{
    [self.listView addSection];
    {
        MKUIElementListViewCellImage *el = [[MKUIElementListViewCellImage alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"图片";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.elementView.detailImage = [Menu randomImage];
        el.whenClick = ^(MKUIElementListViewCellImage *el) {
            el.elementView.subTitleText = [NSString stringWithFormat:@"%@子标题1",el.elementView.subTitleText];
            el.elementView.detailImage = el.elementView.detailImage?nil:[Menu randomImage];
            el.elementView.detailImageSize = CGSizeMake(40, 40);
            el.elementView.detailImageMarggin = UIEdgeInsetsMake(0, 5, 0, 5);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellImage *el = [[MKUIElementListViewCellImage alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"图片";
        el.elementView.subTitleText = @"子标题1";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.elementView.detailImage = [Menu randomImage];
        el.whenClick = ^(MKUIElementListViewCellImage *el) {
            el.elementView.subTitleText = [NSString stringWithFormat:@"%@子标题1",el.elementView.subTitleText];
            el.elementView.detailImage = el.elementView.detailImage?nil:[Menu randomImage];
            el.elementView.detailImageSize = CGSizeMake(40, 40);
            el.elementView.detailImageMarggin = UIEdgeInsetsMake(0, 5, 0, 5);
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
    {
        MKUIElementListViewCellImage *el = [[MKUIElementListViewCellImage alloc] init];
        el.elementView.iconImage = [Menu randomImage];
        el.elementView.titleText = @"图片2";
        el.elementView.accessView = el.elementView.disclosureIndicatorAccessView;
        el.elementView.detailImage = [Menu randomImage];
        el.whenClick = ^(MKUIElementListViewCellImage *el) {
            if(UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, el.elementView.detailImageMarggin)){
                el.elementView.detailImageSize = CGSizeMake(40, 40);
                el.elementView.detailImageMarggin = UIEdgeInsetsMake(0, 5, 0, 5);
            }else{
                el.elementView.detailImageSize = CGSizeMake(28, 28);
                el.elementView.detailImageMarggin = UIEdgeInsetsZero;
            }
            [el mk_reloadInListView];
        };
        [self.listView addItem:el];
    }
}
#pragma mark - delegate:MKUIListViewDelegate
#pragma mark - 定制各分组的属性
- (nullable Class)listView:(MKUIListView *)listView groupBackgroundViewClassForSetion:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupBackgroudViewClass:nil;
}
- (nullable UIColor *)listView:(MKUIListView *)listView groupBackgroundColorForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupBackgroundColor:listView.groupBackgroundColor;
}
- (UIEdgeInsets)listView:(MKUIListView *)listView groupInsetsForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupInsets:listView.groupInsets;
}
- (UIEdgeInsets)listView:(MKUIListView *)listView groupInnerInsetsForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupInnerInsets:listView.groupInnerInsets;
}
- (CGFloat)listView:(MKUIListView *)listView groupCornerRadiusForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_groupCornerRadius:listView.groupCornerRadius;
}
- (CGFloat)listView:(MKUIListView *)listView cellInteritemSpacingForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_cellInteritemSpacing:listView.cellInteritemSpacing;
}

#pragma mark - 定制各分组的分隔线属性
- (BOOL)isCustomSection:(NSInteger)section{
    return YES;
//    if(section>=self.custom_section && section<=self.custom_section+5) return YES;
//    return NO;
}
- (MKUIListViewCellSeparatorStyle)listView:(MKUIListView *)listView separatorStyleForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorStyle:listView.separatorStyle;
}
- (nullable UIColor *)listView:(MKUIListView *)listView separatorColorForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorColor:listView.separatorColor;
}
- (nullable UIColor *)listView:(MKUIListView *)listView separatorBackgroundColorForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorBackgroundColor:listView.separatorBackgroundColor;
}
- (CGFloat)listView:(MKUIListView *)listView separatorSizeForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorSize:listView.separatorSize;
}
- (UIEdgeInsets)listView:(MKUIListView *)listView separatorInsetsForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorInsets:listView.separatorInsets;
}
- (nullable Class)listView:(MKUIListView *)listView separatorViewClassForSection:(NSInteger)section{
    return [self isCustomSection:section]?self.custom_separatorViewClass:listView.separatorViewClass;
}
@end
